package com.lemon.smartsafetynew.util.common;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import static cn.finalteam.toolsfinal.DateUtils.date;


/**
 * Created by Andrew on 2017/8/10.
 */

public class CommonUtil {

    public static boolean isNetAvailable(Context context) {
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context
                        .CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        return (info != null && info.isConnected());
    }


    /**
     * yyyy-MM-dd HH:mm:ss
     * @param time
     * @return
     */
    public static String timeToStr(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_TIME, Locale.getDefault());
        return dateFormat.format(time);
    }


    /**
     * yyyy-MM-dd HH:mm:ss
     * @param time
     * @return
     */
    public static String timeToStr(Date time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_TIME, Locale.getDefault());
        return dateFormat.format(time);
    }

    /**
     * yyyy-MM-dd_HH-mm-ss
     * @param time
     * @return
     */
    public static String timeToStr4(Date time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_TIME4, Locale.getDefault());
        return dateFormat.format(time);
    }

    /**
     * HH:mm:ss
     * @param time
     * @return
     */
    public static String timeToStr2(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_TIME2, Locale.getDefault());
        return dateFormat.format(time);
    }

    /**
     *
     * @param time
     * @param format
     * @return
     */
    public static String timeToStr(Date time, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.getDefault());
        return dateFormat.format(time);
    }

    /**
     *
     * @param time
     * @return
     */
    public static String timeToStr2(Long time) {
        if(time == null) {
            return "无";
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_TIME2, Locale.getDefault());
        return dateFormat.format(time);
    }

    /**
     * timeStr转Date
     * @param time
     * @param format
     * @return
     */
    public static Date timeStrToDate(String time, String format) {

        SimpleDateFormat fmt = new SimpleDateFormat(format, Locale.getDefault());
        Date date = null;
        try {
            date = fmt.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }


    /**
     * @return
     */
    public static Date getNowDate() {
        Calendar now = Calendar.getInstance();
        return now.getTime();
    }

    /**
     * @param “友好显示年月日时分秒”
     * @return
     */
    public static String getFriendTimeStr(Long time) {
        if(time > 0) {
            SimpleDateFormat dateFormat;
            Calendar c = Calendar.getInstance();
            Date date = new Date(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if (c.get(Calendar.YEAR) > calendar.get(Calendar.YEAR)) {
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_TIME3_2, Locale.getDefault());
            } else if (c.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) && c.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)) {
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_TIME3, Locale.getDefault());
            } else {
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_TIME3_1, Locale.getDefault());
            }
            return dateFormat.format(calendar.getTime());
        }else{
            return "";
        }
    }

    /**
     * @param date_type 0: day  1: month 2: year
     * @return
     */
    public static String getNowTimeStr(DateTypeEnum date_type) {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat dateFormat;
        switch (date_type) {
            case DAY:  //day
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
            case MONTH:  //month
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_MONTH, Locale.getDefault());
                break;
            case YEAR:  //year
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_YEAR, Locale.getDefault());
                break;
            default:
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
        }
        return dateFormat.format(now.getTime());
    }

    /**
     * @param time
     * @param date_type 1: day  2: month 3: year
     * @return
     */
    public static Date timeStrToDate(String time, DateTypeEnum date_type) {

        SimpleDateFormat fmt;

        switch (date_type) {
            case DAY:  //day
                fmt = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
            case MONTH:  //month
                fmt = new SimpleDateFormat(TimeFormatConstants.FORMAT_MONTH, Locale.getDefault());
                break;
            case YEAR:  //year
                fmt = new SimpleDateFormat(TimeFormatConstants.FORMAT_YEAR, Locale.getDefault());
                break;
            default:
                fmt = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
        }


        Date date = null;
        try {
            date = fmt.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static String dateToTimeStr(Date date, DateTypeEnum date_type) {

        if (date == null) {
            return "";
        }

        SimpleDateFormat dateFormat;
        switch (date_type) {
            case DAY:  //day
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
            case MONTH:  //month
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_MONTH, Locale.getDefault());
                break;
            case YEAR:  //year
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_YEAR, Locale.getDefault());
                break;
            default:
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
        }
        return dateFormat.format(date);

    }


    public static String secondToTimeStr(long second, DateTypeEnum date_type) {

        Date date = new Date(second);

        SimpleDateFormat dateFormat;
        switch (date_type) {
            case DAY:  //day
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
            case MONTH:  //month
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_MONTH, Locale.getDefault());
                break;
            case YEAR:  //year
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_YEAR, Locale.getDefault());
                break;
            default:
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
        }
        return dateFormat.format(date);

    }

    /**
     *
     * @param second Long
     * @param date_type
     * @return
     */
    public static String secondToTimeStr(Long second, DateTypeEnum date_type) {

        if(second == null) {
            return "";
        }

        Date date = new Date(second);

        SimpleDateFormat dateFormat;
        switch (date_type) {
            case DAY:  //day
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
            case MONTH:  //month
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_MONTH, Locale.getDefault());
                break;
            case YEAR:  //year
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_YEAR, Locale.getDefault());
                break;
            default:
                dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                break;
        }
        return dateFormat.format(date);

    }

    /**
     * @param date
     * @return
     */
    public static Date calendarPlusOne(DateTypeEnum date_type, Date date) {

        if (date == null) {
            return null;
        }

        int field;

        switch (date_type) {
            case DAY:   //day
                field = Calendar.DAY_OF_YEAR;
                break;
            case MONTH: //month
                field = Calendar.MONTH;
                break;
            case YEAR:  //year
                field = Calendar.YEAR;
                break;
            default:
                field = Calendar.DAY_OF_YEAR;
                break;
        }

        Calendar target = Calendar.getInstance();
        target.setTime(date);
        target.add(field, 1);

        return target.getTime();
    }

    /**
     * @param date
     * @return
     */
    public static Date calendarDelOne(DateTypeEnum date_type, Date date) {

        if (date == null) {
            return null;
        }

        int field;

        switch (date_type) {
            case DAY:   //day
                field = Calendar.DAY_OF_YEAR;
                break;
            case MONTH: //month
                field = Calendar.MONTH;
                break;
            case YEAR:  //year
                field = Calendar.YEAR;
                break;
            default:
                field = Calendar.DAY_OF_YEAR;
                break;
        }

        Calendar target = Calendar.getInstance();
        target.setTime(date);
        target.add(field, -1);

        return target.getTime();
    }


    public static boolean compareDate(Date target, Date source) {

        Calendar calendarTarget = Calendar.getInstance();
        calendarTarget.setTime(target);

        Calendar calendarSource = Calendar.getInstance();
        calendarSource.setTime(source);

        if (calendarTarget.compareTo(calendarSource) >= 0) {
            return false;
        } else {
            return true;
        }

    }

    /**
     * 将毫秒时间转换成时分秒
     * @param totalTime
     * @return
     */
    public static String formatTime(int totalTime){
        int hour = 0;
        int minute = 0;
        int second = 0;
        second = totalTime;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        // 转换时分秒 00:00:00
        String duration = (hour >= 10 ? hour : "0" + hour)+ ":" +(minute >= 10 ? minute : "0" + minute)+ ":" +(second >= 10 ? second : "0" + second);
        return duration;
    }

    /**
     *
     * @param totalTime
     * @return
     */
    public static String formatTime(long totalTime){
        long hour = 0;
        long minute = 0;
        long second = 0;
        second = totalTime;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        // 转换时分秒 00:00:00
        String duration = (hour >= 10 ? hour : "0" + hour)+ "小时" +(minute >= 10 ? minute : "0" + minute)+ "分钟";
        return duration;
    }

    /**
     * 保留1位小数
     * @param data
     * @return
     */
    public static String formatData(String data){
        return  new BigDecimal(data).setScale(1, BigDecimal.ROUND_HALF_UP).toString();
    }


    /**
     * 为了解决Gson转换泛型的问题，
     * 我把BigInteger改成了String， BigInteger因为泛型的问题导致精度变成了Float，所以此处我把String的小数点后面去掉
     * @param source
     * @param target
     * @return
     */
    public static boolean stringEqualsForBigInteger(String source, String target) {


        float targetF = Float.parseFloat(target);

        int targetInt = (int)targetF;

        if(source.equals(String.valueOf(targetInt))) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 判断是否为null
     * @param source : Integer
     * @return
     */
    public static String avoidNullString(Integer source) {
        if(source == null) {
            return "";
        } else {
            return String.valueOf(source);
        }
    }

    /**
     * 判断是否为null
     * @param source : Integer
     * @return
     */
    public static String avoidNullString(Long source) {
        if(source == null) {
            return "";
        } else {
            return String.valueOf(source);
        }
    }
    /**
     * 判断是否为null
     * @param source : String
     * @return
     */
    public static String avoidNullString(String source) {
        if(source == null) {
            return "";
        } else {
            return source;
        }
    }

    /**
     * 判断是否为null
     * @param source : Double
     * @return
     */
    public static String avoidNullString(Float source) {
        if(source == null) {
            return "";
        } else {
            return source+"";
        }
    }

    /**
     * 判断是否为null
     * @param source : Double
     * @return
     */
    public static String avoidNullString(Double source) {
        if(source == null) {
            return "";
        } else {
            DecimalFormat df = new DecimalFormat(",###.##");
            return df.format(source) + " 元";
        }
    }

    /**
     * 判断是否为null
     * @param source : Double
     * @return
     */
    public static String avoidNullStringNoTitle(Double source) {
        if(source == null) {
            return "";
        } else {
            return source+"";
        }
    }

    /**
     * 判断是否为null
     * @param source : Double
     * @return
     */
    public static String avoidNullString(Double source, String unit) {
        if(source == null) {
            return "";
        } else {
            return String.valueOf(source + unit);
        }
    }

    /**
     * 判断是否为null
     * @param source : String
     * @return
     */
    public static String avoidNullString(String source, String def) {
        if(source == null) {
            return def;
        } else {
            return source;
        }
    }

    /**
     * 判断是否为null
     * @param source
     * @return
     */
    public static Integer avoidNullString2Zero(Integer source) {
        if(source == null) {
            return 0;
        } else {
            return source;
        }
    }


    //转换dip为px
    public static int convertDipOrPx(Context context, int dip) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    //转换px为dip
    public static int convertPxOrDip(Context context, int px) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f * (px >= 0 ? 1 : -1));
    }

    public static int sp2px(Context context, float spValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 获取UUID
     * @return
     */
    public static String getUUID() {
        return java.util.UUID.randomUUID().toString();
    }

    /**
     * 关闭软键盘
     * @param context 上下文
     */
    public static void hintKb(Activity context) {
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm!= null && imm.isActive() && context.getCurrentFocus() != null){
            if (context.getCurrentFocus().getWindowToken()!=null) {
                imm.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

}
